我想我误解Q承诺是如何工作的。我希望我的第一个承诺能够在下一个开始之前得到解决,但那不会发生。这里是我的代码:符合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'); });
此代码生成:
$ node test.js
starting task one
starting task two
done with task one
done with task two
all done
我希望它产生:
$ node test.js
starting task one
done with task one
starting task two
done with task two
all done
我在做什么错?