Tôi nghĩ rằng tôi hiểu nhầm cách thức hoạt động của các lời hứa Q. Tôi muốn lời hứa đầu tiên của mình để giải quyết trước khi bắt đầu tiếp theo, nhưng điều đó không xảy ra. Đây là mã của tôi:Thực hiện nối tiếp với lời hứa Q
var Q = require('q');
function doWork(taskName) {
var deferred = Q.defer();
console.log('starting', taskName);
setTimeout(function() {
console.log('done with', taskName);
deferred.resolve();
});
return deferred.promise;
}
doWork('task one')
.then(doWork('task two'))
.then(function() { console.log('all done'); });
Mã này sản xuất:
$ node test.js
starting task one
starting task two
done with task one
done with task two
all done
tôi hy vọng rằng nó tạo ra:
$ node test.js
starting task one
done with task one
starting task two
done with task two
all done
Tôi đang làm gì sai?