我看到的代码是这样的:

myObj.doSome("task").then(function(env) {
    // logic
});

那么()从何而来?


当前回答

".then()"函数广泛用于Windows 8 Store应用程序异步编程中的承诺对象。 据我所知,它在某种程度上像一个回调。

在本文档中查找详细信息 http://msdn.microsoft.com/en-us/library/windows/apps/hh700330.aspx

当然,它也可以是任何其他已定义函数的名称。

其他回答

这是我为自己做的一个东西,用来弄清楚事情是如何运作的。我想其他人也会发现这个具体的例子很有用:

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>

我猜doSome会返回这个,也就是myObj,它也有then方法。标准方法链接…

如果doSome不返回this,作为doSome执行的对象,请放心,它将返回具有then方法的某个对象…

正如@patrick指出的,标准js中没有then()

.然后在async函数中返回一个promise。

好的例子是:

var doSome = new Promise(function(resolve, reject){
    resolve('I am doing something');
});

doSome.then(function(value){
    console.log(value);
});

要向它添加另一个逻辑,您还可以添加reject('I am the rejected param')调用函数和console.log。

我大概晚了8年,嗯……不管怎样,我真的不知道then()做什么,但也许MDN可能有一个答案。实际上,我可能更懂一点。

这将向您显示所需的所有信息(希望如此)。除非有人已经发布了这个链接。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

格式为promise.prototype.then() promise和prototype有点像变量,但不像javascript中的变量,我的意思是像navigator。getbattery()。then()这样的东西,这个确实存在,但很少在网络上使用,这个显示设备电池的状态,如果你好奇,MDN上还有更多信息。

".then()"函数广泛用于Windows 8 Store应用程序异步编程中的承诺对象。 据我所知,它在某种程度上像一个回调。

在本文档中查找详细信息 http://msdn.microsoft.com/en-us/library/windows/apps/hh700330.aspx

当然,它也可以是任何其他已定义函数的名称。