我看到的代码是这样的:

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

那么()从何而来?


当前回答

.然后在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。

其他回答

这是关于在箭头函数中使用花括号{}的问题:


这3个例子做的是同样的事情(什么都没有,但是有有效的语法,并且是一个有效的Promise链!)


新承诺(函数(ok) { ok ( /* myFunc1(param1, param2, ..) */ ) })(函数(){ /* myFunc1成功*/ /*启动其他内容*/ /* console.log(whateverparam1) */ /* myFunc2(whateverparam1, otherparam, ..) */ })(函数(){ /* myFunc2 succeed */ /*启动其他内容*/ /* myFunc3(whatever38, ..) */ }) console.log(“这段代码没有错误GG!”)


相同的逻辑使用箭头函数省略{}

new Promise((ok) => ok( /* myFunc1(param1, param2, ..) */ ).then(() => 0 // HEY DID YOU NOTICE! A number that does nothing, // but otherwise the parsing will fail! // The code is pretty clean but have a major downside // As arrow functions without {} can contains only one declaration // console.log("something") will FAIL here ).then(() => "" // HEY DID YOU NOTICE! An empty string that does nothing, // but otherwise the parsing will fail! // As arrow functions without {} can contains only one declaration // We can't add more code here, hence: // console.log("something") // Will break the whole promise // This is likely the error in y(our) code ;) )) console.log("This code has no errors GG!")


带有{}的箭头函数

新承诺((ok) => { ok ( /* myFunc1(param1, param2, ..) */ ) })。那么(()=> { /* myFunc1成功*/ /*启动其他内容*/ })。那么(()=> { /* myFunc2 succeed */ /*启动其他内容*/ /* myFunc3(whatever38, ..) */ console.log(“什么”) /*更多控制台日志!* / console.log(“其他东西”) }) console.log(“这段代码没有错误GG!”)

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

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

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

在这种情况下,then()是doSome()方法返回的对象的类方法。

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

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>

.然后在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。