ReactJsを使用してアトムシェルアプリを作成しています。したがって、私は書き2 simple node.js-modules
ました。1 つ目は XML API をフェッチし、オブジェクトの配列を返します。2 つ目は単純なデータストアで、取得したデータセットを保存する必要があります。両方のモジュールは非同期を利用してbluebird
います。
今、私は次のシナリオを持っています:
var Promise = require('bluebird');
store.get()
.tap(print) //#1: []
.then(function (data) {
if (data.length) {
return Promise.resolve(data); //#6: ? also true in the second run ?
} else {
return xmlApi.get()
.tap(print) //#2: [...]
.then(store.insert) // <-- this returns an array with inserted indices
.tap(print) //#3: [...]
-then(store.write) // <-- this returns all stored objects as array
.tap(print) //#4: true <-- ? this is, what I doesn't understand... ?
}
})
.tap(print) //#5: true
.then(function (data) {
//Under normal circumstances, I would set ReactJs state from data array...
})
.catch(handleError)
.done();
私の問題は理解することです.なぜステップ#3を超えてすべてがtrue
代わりに解決されるのarrays with values
ですか?純粋なノードでライブラリをテストする場合、すべて問題ありません。しかし、ストアがデータを保持する 2 回目の実行でも、promise はtrue
...に解決されます。
アップデート:
store.write
var write = Promise.promisify(require('fs').writeFile)
Store.prototype.write = function () {
return write(this.filename, JSON.stringify(this.data))
.bind(this)
.then(function () {
return Promise.resolve(this.data);
});
};