最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to resolve promises when using app with REPL - Stack Overflow

programmeradmin3浏览0评论

I've got a basic Node webserver (Koa.js + a ORM). I like to start it with a REPL meaning I can use my app like a CLI-tool.

All my queries return Promises but I don't know how I can resolve them in the REPL. How can I resolve them?

For example the following code (fetch() queries the database and returns a promise) gives only this output Promise {_bitField: 4325376, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined …}

Transaction.where('reference', '1').fetch().then((res) => return res)

I've got a basic Node webserver (Koa.js + a ORM). I like to start it with a REPL meaning I can use my app like a CLI-tool.

All my queries return Promises but I don't know how I can resolve them in the REPL. How can I resolve them?

For example the following code (fetch() queries the database and returns a promise) gives only this output Promise {_bitField: 4325376, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined …}

Transaction.where('reference', '1').fetch().then((res) => return res)
Share Improve this question asked May 24, 2017 at 22:34 HedgeHedge 16.7k45 gold badges153 silver badges260 bronze badges 1
  • 1 This looks very much like a Bluebird promise. In that case, you can do _.value() in the following line – Bergi Commented May 24, 2017 at 23:25
Add a comment  | 

3 Answers 3

Reset to default 14

Update: Node.js now does this by default and resolves promises


Old answer:

You can't resolve them proper - but you can extract their references to the global scope:

> Transaction.where('reference', '1').fetch().then((res) => out = res)
[Object Promise]
> out
  /* your data outputted here since the global was assigned to*/

We might allow await in the REPL in the future in Node which would solve the issue more cleanly.

Just setting a global return value may (and often will) show wrong results if not waiting for the promise fulfillment.

To ensure the user have a fulfilled promise you may provide your own evaluator to the repl server:

// sample-repl.js
const repl=require('repl');
function replEvalPromise(cmd,ctx,filename,cb) {
  let result=eval(cmd);
  if (result instanceof Promise) {
    return result
      .then(response=>cb(null,response));
  }
  return cb(null, result);
}
repl.start({ prompt: 'promise-aware> ', eval: replEvalPromise });

Such REPL only returns control to the user after the promise got resolved:

$ node sample-repl.js
promise-aware> new Promise(resolve=>setTimeout(()=>resolve('Finished!'),5000));
'Finished!'
promise-aware> out = new Promise(resolve=>setTimeout(()=>resolve('Finished!'),5000));
'Finished!'
promise-aware> out
'Finished!'
promise-aware>

Note as it sets the proper global return variable with the resolved value.

The standard node REPL works like that instead:

> out = new Promise(resolve=>setTimeout(()=>resolve('Finished!'),5000));
Promise { <pending> }
> out
Promise { <pending> }
> out
Promise { <pending> }
> out
Promise { <pending> }
> out
Promise { 'Finished!' }
>

There are userland packages that implement this, for example https://github.com/skyrising/await-repl and https://github.com/StreetStrider/repl.js

发布评论

评论列表(0)

  1. 暂无评论