65847

Node.js: promisifying callback library for 'yield' keyword

I'm trying to build a simple REST API with Koa.js. It uses ES6 generator functions, which I find much more pleasant than callbacks (they're just like C#'s async-await). The yield keyword expects a thenable (promise, thunk, generator). I'm using Bluebird's promisifyAll method to promisify callback libraries (request in my case), but I still keep getting error. Here are my code and the error:

var koa = require('koa') , route = require('koa-route') , app = module.exports = koa() , Promise = require('bluebird') , request = Promise.promisifyAll(require('request')); app.use(route.get('/users', list)); function *list() { var res = yield request.get('http://backbonejs-beginner.herokuapp.com/users'); this.body = res.body; } app.listen(3000);

Stack trace for the error:

Error: yield a function, promise, generator, array, or object at next (/Users/jashua/Desktop/node_modules/koa/node_modules/co/index.js:109:12) at Object.<anonymous> (/Users/jashua/Desktop/node_modules/koa/node_modules/co/index.js:50:5) at next (/Users/jashua/Desktop/node_modules/koa/node_modules/co/index.js:93:21) at Object.<anonymous> (/Users/jashua/Desktop/node_modules/koa/node_modules/co/index.js:50:5) at Server.<anonymous> (/Users/jashua/Desktop/node_modules/koa/lib/application.js:121:8) at Server.EventEmitter.emit (events.js:107:17) at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:504:12) at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23) at Socket.socketOnData (_http_server.js:357:22) at Socket.EventEmitter.emit (events.js:104:17)

What am I missing?

Answer1:

Bluebird adds an Async suffix to promisified functions with promisifyAll.

Try:

var res = yield request.getAsync('http://backbonejs-beginner.herokuapp.com/users');

Recommend

  • Promisify cursor execution: MongoDB Native Driver
  • How to promisify a braintree method?
  • Standard way to do asyntask in order with promise
  • type infererence with overloaded functions
  • Api Gateway cannot allow Access-Control-Allow-Origin
  • Trouble Attaching Call Back to Unobtrusive Validation Show Error
  • While loop won't end when I tell it in JavaScript
  • JavaScript overloading with a callback
  • Netlink sockets and libnl - nl_recvmsgs_default returning -16 (EBUSY)
  • Creating a C++ function that calls other Lua function
  • pythonanywhere - How do I use websockets to transmit messages as per the web2py messaging example?
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • Defined variables not working in javascript files when I use getScript
  • Can't remove headers after they are sent
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Trying to switch camera back to front but getting exception
  • Symfony2: How to get request parameter
  • How to make Safari send if-modified-since header?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How to pass list parameters for each object using Spring MVC?
  • How to delete a row from a dynamic generate table using jquery?
  • log4net write single file for each call to log.info
  • Acquiring multiple attributes from .xml file in c#
  • Free memory of cv::Mat loaded using FileStorage API
  • Getting error when using KSoap library to consume .NET web services
  • using HTMLImports.whenReady not working in chrome
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • EntityFramework adding new object to nested object collection
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • JaxB to read class hierarchy
  • Programmatically clearing map cache
  • java string with new operator and a literal