From 0fd6f30b9b50800d1d9ff32cac76ff9fb8c75408 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Tue, 15 Oct 2019 12:14:08 -0600 Subject: [PATCH] fix(captp): be sure to bind the questionID, promiseID By using lastPromiseID directly, it could be incremented before the promise actually resolves, which results in an incorrect message. --- lib/captp.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/captp.js b/lib/captp.js index 0991e0abc6e..d380c50b881 100644 --- a/lib/captp.js +++ b/lib/captp.js @@ -29,18 +29,19 @@ export default function makeCapTP(ourId, send, bootstrapObj = undefined) { // new export if (Promise.resolve(val) === val) { lastPromiseID += 1; - slot = `p${lastPromiseID}`; + const promiseID = lastPromiseID; + slot = `p${promiseID}`; val.then( res => send({ type: 'CTP_RESOLVE', - promiseID: lastPromiseID, + promiseID, res: serialize(harden(res)), }), rej => send({ type: 'CTP_RESOLVE', - promiseID: lastPromiseID, + promiseID, rej: serialize(harden(rej)), }), ); @@ -80,10 +81,11 @@ export default function makeCapTP(ourId, send, bootstrapObj = undefined) { pr.rej = reject; }); lastQuestionID += 1; - questions.set(lastQuestionID, pr); + const questionID = lastQuestionID; + questions.set(questionID, pr); send({ type: 'CTP_CALL', - questionID: lastQuestionID, + questionID, target: slot, method: serialize(harden([prop, args])), }); @@ -184,10 +186,11 @@ export default function makeCapTP(ourId, send, bootstrapObj = undefined) { pr.rej = reject; }); lastQuestionID += 1; - questions.set(lastQuestionID, pr); + const questionID = lastQuestionID; + questions.set(questionID, pr); send({ type: 'CTP_BOOTSTRAP', - questionID: lastQuestionID, + questionID, }); return harden(pr.p); };