我有一个功能:
const Q = require('q');
const timers = require('timers');
function _testQ() {
const deferred = Q.defer();
console.log('test timer event 1s.');
let t = timers.setInterval(() => {
console.log('Timer event!');
timers.clearInterval(t);
deferred.resolve(true);
}, 1000);
return deferred.promise;
}
let out = _testQ().then((n) => {
console.log('Debug1 n:', n);
});
console.log('Debug2 ', out);
如何以syncronios方式调用它。
我得到这样的输出:
test timer event 1s.
Debug2 { state: 'pending' }
Timer event!
Debug1 n: true
我需要它像这样打印:
test timer event 1 s.
Timer event!
Debug1 n: true
Debug2 {state: 'pending'}
答案 0 :(得分:1)
如果你想在Debug1之后执行Debug2,你需要一个额外的.then块
let out = _testQ()
.then((n) => {
console.log('Debug1 n:', n);
})
.then(() => {
console.log('Debug2 ');
});