[英]Nodejs promise not working when using q library
我试图独自学习诺言。 这是我写的代码-
var Q = require('q');
var promise = Q.fcall(function() {
// I expect this time out to delay returning the response 7.
setTimeout( console.log('hi'), 1000 );
return 7;
});
promise.then(function(contents) {
console.log(contents);
});
// Added this timeout so that the javascript execution context(node ex.js) remains alive before the code in the then block is resolved.
setTimeout( function(){console.log('bye');}, 1000 );
现在,这不是打印内容。 我刚得到C:\\ node \\ Ex_Files_UaR_Node \\ first> node example3.js嗨,再见
我期待得到-嗨7再见
请让我知道我是否缺少任何明显的东西。
编辑:
PS下面的代码实现了承诺-
var Q = require('q');
var promise = Q.fcall(function() {
setTimeout( function(){console.log('hi');}, 1000 );
return 7;
});
promise.then(function(contents) {
console.log(contents);
});
setTimeout( function(){console.log('bye');}, 1000 );
但是,在Q.fcall中添加setTimeOut的基本思想是延迟Promise的执行。 知道怎么做吗?
你去
let p = new Promise((resolve, reject) => {
setTimeout(() => resolve(4), 2000);
});
p.then((res) => {
console.log(res);
});
ES6承诺更简单。 p仅在2秒后解析。 直到那时,它仍然没有解决,也没有被拒绝。 因此,仅在2秒后执行
您的第一个日志语句有问题。
我已修改您的代码以使其正常工作。
var Q=require('q');
var promise = Q.fcall(function() {
setTimeout( function(){console.log('hi')}, 1000 );
return 7;
})
promise.then(function(contents) {
console.log(contents);
});
setTimeout(function(){console.log('bye');}, 1000 );
但是此代码将按各自的顺序打印7,bye和hi,因为以下动作是同时发生的:
fcall()
。 then()
方法注册处理程序。 现在,以下事情以下列顺序异步发生:
fcall()
回调。 希望它使流程清楚。
我无法重现未在输出中得到7的问题(即使您的代码有几个逻辑错误),因为此代码段(不进行任何更正) 确实会产生它:
// This is browser version, so `require` is replaced by `script src` tag. //var Q = require('q'); var promise = Q.fcall(function() { // I expect this time out to delay returning the response 7. setTimeout( console.log('hi'), 1000 ); return 7; }); promise.then(function(contents) { console.log(contents); }); // Added this timeout so that the javascript execution context(node ex.js) remains alive before the code in the then block is resolved. setTimeout( function(){console.log('bye');}, 1000 );
<script src="https://cdnjs.cloudflare.com/ajax/libs/q.js/1.5.0/q.js"></script>
但是,有几个问题:
setTimeout
的第一个参数不是函数:您将其传递给console.log('hi')
而不是function(){console.log('hi');}
。 return 7
将立即执行。 如果您希望延迟初始承诺的履行,则Q.fcall
不是您需要的方法:您需要Q.Promise
。 以下是您的预期行为的编码方式:
var promise = Q.Promise(function (resolve, reject) { setTimeout( function () { console.log('hi'); resolve(7); }, 1000 ); }); promise.then(function(contents) { console.log(contents); }); setTimeout( function(){console.log('bye');}, 1000 );
<script src="https://cdnjs.cloudflare.com/ajax/libs/q.js/1.5.0/q.js"></script>
请注意,Node具有本机的Promise库,您可以使用该库执行相同的操作。 确实不需要添加Q。只需将Q.Promise(...)
替换为new Promise(...)
:
var promise = new Promise(function (resolve, reject) { setTimeout( function () { console.log('hi'); resolve(7); }, 1000 ); }); promise.then(function(contents) { console.log(contents); }); setTimeout( function(){console.log('bye');}, 1000 );
请注意,“ 7”的输出顺序略有不同。 这是因为Promises的本机实现使用微任务来调度then
回调的执行,而Q
使用主队列上的事件来调度该执行,稍后再执行。 许多人会认为本地行为“更正确”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.