我看到的代码是这样的:
myObj.doSome("task").then(function(env) {
// logic
});
那么()从何而来?
我看到的代码是这样的:
myObj.doSome("task").then(function(env) {
// logic
});
那么()从何而来?
当前回答
这是我为自己做的一个东西,用来弄清楚事情是如何运作的。我想其他人也会发现这个具体的例子很有用:
doit().then(function() { log('Now finally done!') }); log('---- But notice where this ends up!'); // For pedagogical reasons I originally wrote the following doit()-function so that // it was clear that it is a promise. That way wasn't really a normal way to do // it though, and therefore Slikts edited my answer. I therefore now want to remind // you here that the return value of the following function is a promise, because // it is an async function (every async function returns a promise). async function doit() { log('Calling someTimeConsumingThing'); await someTimeConsumingThing(); log('Ready with someTimeConsumingThing'); } function someTimeConsumingThing() { return new Promise(function(resolve,reject) { setTimeout(resolve, 2000); }) } function log(txt) { document.getElementById('msg').innerHTML += txt + '<br>' } <div id='msg'></div>
其他回答
这是我为自己做的一个东西,用来弄清楚事情是如何运作的。我想其他人也会发现这个具体的例子很有用:
doit().then(function() { log('Now finally done!') }); log('---- But notice where this ends up!'); // For pedagogical reasons I originally wrote the following doit()-function so that // it was clear that it is a promise. That way wasn't really a normal way to do // it though, and therefore Slikts edited my answer. I therefore now want to remind // you here that the return value of the following function is a promise, because // it is an async function (every async function returns a promise). async function doit() { log('Calling someTimeConsumingThing'); await someTimeConsumingThing(); log('Ready with someTimeConsumingThing'); } function someTimeConsumingThing() { return new Promise(function(resolve,reject) { setTimeout(resolve, 2000); }) } function log(txt) { document.getElementById('msg').innerHTML += txt + '<br>' } <div id='msg'></div>
这是一个小的JS_Fiddle。
然后是一个方法回调堆栈,在承诺被解决后可用,它是库的一部分,如jQuery,但现在在原生JavaScript中可用,下面是它如何工作的详细解释
你可以在原生JavaScript中做一个承诺:就像在jQuery中有承诺一样,每个承诺都可以堆叠,然后可以用Resolve和Reject回调调用,这就是你如何链式异步调用。
我从MSDN文档中分叉和编辑电池充电状态。
这是试图找出用户的笔记本电脑或设备是否正在充电电池。然后是叫你和你的工作成功后。
navigator
.getBattery()
.then(function(battery) {
var charging = battery.charging;
alert(charging);
})
.then(function(){alert("YeoMan : SINGH is King !!");});
另一个es6例子
function fetchAsync (url, timeout, onData, onError) {
…
}
let fetchPromised = (url, timeout) => {
return new Promise((resolve, reject) => {
fetchAsync(url, timeout, resolve, reject)
})
}
Promise.all([
fetchPromised("http://backend/foo.txt", 500),
fetchPromised("http://backend/bar.txt", 500),
fetchPromised("http://backend/baz.txt", 500)
]).then((data) => {
let [ foo, bar, baz ] = data
console.log(`success: foo=${foo} bar=${bar} baz=${baz}`)
}, (err) => {
console.log(`error: ${err}`)
})
then是一个用于解决异步回调的方法 这是在ES6中引入的 请在这里找到适当的文件Es6承诺
我猜doSome会返回这个,也就是myObj,它也有then方法。标准方法链接…
如果doSome不返回this,作为doSome执行的对象,请放心,它将返回具有then方法的某个对象…
正如@patrick指出的,标准js中没有then()
doSome("task")必须返回一个promise对象,而这个promise总是有一个then函数,所以你的代码就像这样
promise.then(function(env) {
// logic
});
这只是一个普通的成员函数调用。
".then()"函数广泛用于Windows 8 Store应用程序异步编程中的承诺对象。 据我所知,它在某种程度上像一个回调。
在本文档中查找详细信息 http://msdn.microsoft.com/en-us/library/windows/apps/hh700330.aspx
当然,它也可以是任何其他已定义函数的名称。