所以,我们有这样的东西:
function thePromise(){
var thePromise = new Promise(function(){
var data = 'foo';
console.log(data);
resolve();
});
return thePromise;
}
thePromise().then( function(){
console.log('bar');
});
console.log('foobar');
人们希望输出的顺序为foo, bar, foobar
,但最终总是以foo, foobar, bar
结尾。
除了使用setTimeout()
之外,还有一种方法可以确保在执行下一行代码之前完成.then()
中的代码吗?我正在尝试做一些数据库操作并与远程服务器同步,因此使用setTimeout()
是一个冒险的提议,因为它可能需要花费不确定的时间才能解决。
答案 0 :(得分:1)
只需兑现您的承诺:
var promise = new Promise(function(resolve, reject){
resolve(console.log('foo'));
});
var fctn = function(){
console.log('bar');
}
promise.then(fctn).then(function(){
console.log('foobar');
});
答案 1 :(得分:0)
then()返回一个Promise,因此您可以链接:
/etc/apache2/sites-enabled/000-default
<Directory /var/www/your-project/>
Options Indexes FollowSymLinks MultiViews
DirectoryIndex index.php
DirectoryIndex index.html
AllowOverride None
Order allow,deny
allow from all
</Directory>
答案 2 :(得分:-1)