如果你用另一个承诺解决承诺会怎么样?

时间:2016-01-23 14:42:14

标签: javascript promise ecmascript-6 es6-promise

ECMAScript 6.0规范对Promise解析另一个Promise的说法是什么?是否应该通过将Promise附加到可以解决此问题的then来采用其他Promise的状态?

我在Chrome中尝试了这个代码段,这就是我得到的内容,而Promise1 Promise2> Promise1 = new Promise(function(resolve){ResolveYouLater1 = resolve}) > Promise2 = new Promise(function(resolve){ResolveYouLater2 = resolve}) > ResolveYouLater1(Promise2) undefined > Promise1 Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: Promise} 的解决方案似乎很好吗?

then

我认为应该将ResolveYouLater1(value)附加到Promise2,Promise2#include <thread> #include <functional> #include <vector> #include <iostream> #include <memory> #include <cassert> struct MsgFromAgent { std::string str; }; struct MsgToAgent { std::string str; }; struct IClient { virtual void onMsgFromBC(MsgFromAgent& msg) = 0; virtual void onTimeout() = 0; virtual ~IClient() {}; }; struct Engine { Engine(IClient& client) : isRunning(false), client(client) { } ~Engine() { // we expect that if client started the engine // client must stop it before Engine will be destructed assert(!isRunning && "Running engine should be stoped by the client"); // in any way, handle exceptional situation if it arises if(isRunning) { isRunning = false; th.join(); } } void start() { isRunning = true; th = std::thread(std::bind(&Engine::eventLoop, this)); } bool write(const MsgToAgent& msg) { // some code } void stop() { isRunning = false; th.join(); } void eventLoop() { while(isRunning) { // blocking calls, that return control when certain interrupts from hardware arrives // if interrupt haven't arraived within certain time limit: try { client.onTimeout(); } catch(...) { } // interrupt about incomming msg: MsgFromAgent dummyMsg; try { client.onMsgFromBC(dummyMsg); } catch (...) { } } } bool isRunning; std::thread th; IClient& client; }; struct Client : public IClient { Client() : e(*this) { }; ~Client() { } void onMsgFromBC(MsgFromAgent& msg) { std::cout << "On message\n"; } void onTimeout() { std::cout << "On timeout\n"; } Engine e; }; int main() { Client c; return 0; } 结算时应该dbx.files_upload(data, '/file.py', mode=dropbox.files.WriteMode.overwrite)

1 个答案:

答案 0 :(得分:1)

  

ECMAScript 6.0规范对Promise解决另一个Promise有什么看法?

你可以在这里自己阅读:http://www.ecma-international.org/ecma-262/6.0/#sec-promise-resolve-functions
当参数是一个可以参数时,PromiseResolveThenableJob将排队。

  

它是否应该采用另一个Promise的状态,然后将一个Promise附加到Promise上来解决这个问题?

是。 {/ 1}}方法将使用解析器和拒绝函数调用。

  

我在Chrome中尝试过这个代码片段,这就是我得到的,似乎只是用Promise2来解决Promise1就好了吗?

是。现在只有resolved

我必须承认,我目前的Chrome版本(48.0.2564.82,V8 4.8.271.17)并不完全符合此算法(本身可能不是坏事,avoids memory problems但是可能出乎意料) 它是懒惰的,只有在实际回调对结果感兴趣时才调度作业。并且它不会将解析器传递给.then()方法,而是通过想要知道结果的实际回调。

then

> var resolve1, resolve2;
> var promise1 = new Promise(function(r){ resolve1 = r; });
> var promise2 = new Promise(function(r){ resolve2 = r; });
> promise2.then = function(...args) { console.log(...args);
>                                     return Promise.prototype.then.call(this, ...args); };
> resolve1(promise2);
  undefined
// you'd expected a call to the then method here
> promise1
  Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: Promise}
> promise2
  Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
> resolve2(42)
  undefined
// or at least here
> promise1
  Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: Promise}
> promise2
  Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: 42}
> promise1.then(x => console.log(x))
  x => console.log(x), PromiseIdRejectHandler() { [native code] }
  // but it only is here
  42
  Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
> promise2.then(x => console.log(x))
  x => console.log(x)
  42
  Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
> promise1.then(x => console.log(x))
  x => console.log(x), PromiseIdRejectHandler() { [native code] }
// hell they're even calling it multiple times!
42
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}