gue*_*314 -6 javascript promise
特定
function doStuff(n /* `n` is expected to be a positive number */) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(n * 10)
}, Math.floor(Math.random() * 1000))
})
.then(function(result) {
if (result > 100) {
console.log(result + " is greater than 100")
} else {
console.log(result + " is not greater than 100");
}
})
}
doStuff(9)
.then(function(data) {
console.log(data) // `undefined`, why?
})Run Code Online (Sandbox Code Playgroud)
为什么要被拴data undefined在一起打电话?.then()doStuff()
gue*_*314 13
因为从链接到构造函数没有Promise或其他值.return.then()Promise
请注意,.then()返回一个新Promise对象.
解决方案是对值或来自return的值或其他函数调用.returnPromise.then()
function doStuff(n /* `n` is expected to be a positive number */) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(n * 10)
}, Math.floor(Math.random() * 1000))
})
.then(function(result) {
if (result > 100) {
console.log(result + " is greater than 100")
} else {
console.log(result + " is not greater than 100");
}
// `return` `result` or other value here
// to avoid `undefined` at chained `.then()`
return result
})
}
doStuff(9)
.then(function(data) {
console.log("data is: " + data) // `data` is not `undefined`
});Run Code Online (Sandbox Code Playgroud)
问题正面临着:
return
(new Promise(..)) //the promise we want to return
.then(()=>undefined) // the promise were actually returning, which resolves to undefined
Run Code Online (Sandbox Code Playgroud)
您可能已经注意到,然后返回一个新的承诺。这有一个很好的理由,它使promise链接变得容易,例如:
getUser()//an asynchronous action
.then(user=>login(user))//then if we get the user,promise to log in
.then(token=>console.log("logged in,token is "+token) //then if we logged in, log it
.catch(error=>"login failed");//catch all errors from above
Run Code Online (Sandbox Code Playgroud)
但这也造成了我们面临的小陷阱。解决方案可能是返回原始的Promise,而不是.then()自动返回的新Promise,因为这被解析为undefined,因为里面的函数没有显式返回任何东西:
//what were doing:
Promise.resolve(n*10)//the original promise resolves to n*10
.then(a=>undefined)//the then gets n*10 passed as a, but returns undefined
.then(b=>console.log(b));//b will be undefined :0
//what we want:
var promise=Promise.resolve(n*10);
promise.then(a=>undefined);//a is n*10, this resolves to undefined
promise.then(b=>console.log(b));//but this still logs n*10, as its the original promise :)
Run Code Online (Sandbox Code Playgroud)
如您所见,要返回原始的Promise,我们只需将其存储在变量中,然后为其分配.then处理程序,并仍然引用原始的Promise,我们可以将其分配给其他处理程序(或return)。
实际上:
function doStuff(n /* `n` is expected to be a number */) {
//create a new promise and store it
var promise=new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(n * 10)
},1000);
});
//add a then handler to this promise
promise.then(result=>console.log(result + " is "+result<100?"not":""+" greater than 100"));
//return the original one
return promise;
}
doStuff(9).then(function(data) {
console.log(data) //not undefined, as original promise
})
Run Code Online (Sandbox Code Playgroud)
doStuff 正在返回Promise. 但是,您的最后一个then函数没有返回任何值,因此data作为undefined.
在 Promise 中,下一个then函数的参数值是前一个函数的返回值then。
function doStuff(n /* `n` is expected to be a positive number */) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(n * 10)
}, Math.floor(Math.random() * 1000))
})
.then(function(result) {
if (result > 100) {
console.log(result + " is greater than 100")
} else {
console.log(result + " is not greater than 100");
}
return result;
})
}
doStuff(9)
.then(function(data) {
console.log(data) // `90`
})Run Code Online (Sandbox Code Playgroud)