簡體   English   中英

使用Q庫時Node.js承諾不起作用

[英]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()
  • promise變量使用then()方法注冊處理程序。
  • 旨在登錄“再見”的超時功能已注冊。

現在,以下事情以下列順序異步發生:

  • 您注冊的promise回調(您將在其中注冊超時函數以打印“ hi”並返回值7)稱為fcall()回調。
  • 現在,setTimeout注冊了旨在記錄“ hi”的函數並返回值7。
  • Promise將使用值7進行解析,並將該值記錄到控制台。
  • “再見”的超時時間已過期,並將其記錄到控制台。
  • “ hi”的超時已過期並將其記錄到控制台。

希望它使流程清楚。

我無法重現未在輸出中得到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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM