我完全重寫我的問題,甚至進一步煮沸它。nodejs承諾只是不等待
爲什麼,在下面的代碼,確實x()
等同於undefined
代替success
其通過console.log('success');
最後一行結束在控制檯執行記錄到控制檯;那麼將觸發.then()
回叫。
我該如何做到這一點,x()
在最後一行開始執行之前返回'成功'值。
即使yF()評估爲undefined
。但是,然後()回顯th y: success
。
const promise = require('promise');
const requestify = require('requestify');
function x() {
requestify.get('https://<redacted>/')
.then(function (d) {
console.log('success', d.code);
return 'success';
})
.fail(function (f) {
console.log('fail', f.code);
return 'fail';
});
;
}
var yF = function() {
yP .then(function (th) { console.log("th", th); return th; })
.catch(function (fl) { console.log("fl", fl); return fl; });
}
var yP = new Promise(
function (resolve, reject) {
if (1 == 1) {
resolve("y: success");
} else {
reject(new Error("y: fail"));
}
}
);
console.log("hello", x());
console.log("world", yF());
請進一步解釋。你是說在requestify.get的then子句中的代碼在請求完成之前被觸發? – user93
您是否在完成所有操作之後在內部完成請求 – user93