Support Promise.resolve(null) to return next
See original GitHub issueI’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:
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:
- Created 8 years ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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)I think your solution would be to change:toThat is what I do if I have to call stuff likenext
ordone
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:Effectively what you’re with this code: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.