question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Promise.resolve(null) to return next

See original GitHub issue

I’ve made a library and I’m trying to support both express’ router and this one.

The value from the promise function returns the value of next.

The problem is that the when next() is passed “next” as in next("next") it receives it as an error.

Ideally I can run code like this:

For express-promise-router.

var promiseRouter = require("express-promise-router")()
promiseRouter.get("/hello-two", function(req, res){
  return somePromise()
})

For express’ router

var expressRouter = require("express").Router()
expressRouter.get("/hello-one", function(req, res, next){
  return somePromise().then(next)
})

Ideally Promise.resolve(null) was supported to return next as well as next, that way I can pass the value null into next and get the next route.

Promise.resolve(null).then(next) // no error
Promise.resolve("next").then(next) // error

Currently I have to write this & don’t worry about errors because they’re caught and returned.

var expressRouter = require("express").Router()
expressRouter.get("/hello-one", function(req, res, next){
  return somePromise().then(function(val){
    if(val == "next") return next()
    return next(val)
  })
})

Thoughts?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
TheThingcommented, Mar 17, 2016

Oh wow, I just took a better look at the code and now I realize what your problem is. The problem that I see though with making Promise.resolve(null) call next is routes will probably most likely return null under normal circumstances.

Also I think you should be be careful with calling next(val) from a resolved promise. Especially considering routes might accidentally return values from a successful promise (especially with arrow functions)

1reaction
TheThingcommented, Mar 17, 2016

I think your solution would be to change:

return somePromise().then(next)

to

return somePromise().then(() => next())

That is what I do if I have to call stuff like next or done from promises.

Also, next is primarily being used to be called either empty (no value = everything was success) or with an error (parameter = something went wrong) so passing it directly into then like .then(next) would be serious anti pattern as anything that gets resolved, any value or anything will suddenly be interpreted as an error. The more correct pattern would be:

return somePromise().then(() => next()).catch(next)

Effectively what you’re with this code:

return somePromise().then(function(val){
    if(val == "next") return next()
    return next(val)
  })

Is turning any and all values (excluding the literal value "next" that is being resolved (remember, resolved = success) as a failure which is pretty serious antipattern as I mentioned.

Edit: See new reply below.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise.resolve() - JavaScript - MDN Web Docs
In brief, Promise.resolve() returns a promise whose eventual state depends on another promise, thenable object, or other value.
Read more >
Promise resolves to null when resolving this - Stack Overflow
I can't figure out why Promise. resolve returns null when I'm resolving with a constant containing the this context.
Read more >
JavaScript promise resolve() Method - GeeksforGeeks
Promise resolve () method: The promise.resolve() method in JS returns a Promise object that is resolved with a given value.
Read more >
How to use resolve function in Promise - Javascript - Tabnine
it('returning Promise', done => { const vm = new Vue({ template: '<div><test></test></div>', components: { test: () => { return new Promise(resolve ...
Read more >
JavaScript Promise Tutorial – How to Resolve or Reject ...
Promise.race([promises]) – It waits for the first (quickest) promise to settle, and returns the result/error accordingly. Promise ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found