Callbacks take a variable number of arguments. The first argument, go , is a function to call to pass execution to the next pledge in the chain. The go function may be called with any number of arguments, which will be passed to the next pledge in the chain similar to returning a value from promise.
Any callback arguments after the first are passed in from the previous pledge in this manner. If any arguments passed to go are Pledge instances, they will be resolved before the next pledge in the chain is resolved. If any arguments passed to go are "thenable," they will be wrapped in pledges and resolved before the next pledge in the chain is resolved. This should allow pledges to integrate seamlessly with interfaces that return promises.
As a syntactic sugar, a callback may return a truthy value rather than calling go. This is equivalent to calling go with that value as the sole argument. There are a few experimental features that aren't really the core focus of the library, but might stick. Most notably, if you pass and multiple callback arguments, the next item in the chain will only resolve when they are all resolved.
When using multiple callbacks, the next item in the chain will receive go plus one argument for each callback, containing a list of all arguments passed to go from that callback. The go function is useful because it can pass multiple values to the next entity in the chain, while return can only pass a single value. It's also useful for using callback-style interfaces without much hassle.
With promises, this kind of thing gets a little more ugly. You end up wanting to use a promise-aware image loader. This is where promises work as a design contract If you just want to do ad-hoc stuff without using special interfaces, you have to do something awkward like this:. However, this only works because onload passes the callback a single argument, as resolve only accepts a single argument.
To use promises with something that passes the callback multiple arguments, you'd have to do even more work:.
There is no need for a special file loader that returns pledges; we can use pledges with existing callback mechanisms easily. It's sort of rough in some parts as I'm still developing the idea, but here it is. I'm more interested in having the overall design of the library reviewed than the code itself, but comments on the code are of course welcome.
From a high level perspective, the code gets a bit hairy from Internal stuff on, which means the API is quite clear. This code could have been shorter if the Pledge constructor could take the vows parameter:. I dont understand from the code why you need this, and the comments are not helping me understand this either. You have this array shifting in both argValue and listValue , you could have a common helper function here.
Dagg 3, 17 Don't nest promises inside of promises. Its flavour ripens as the length dwindles. Since they are method names, they must be valid JS identifiers and thus don't need to be quoted. Jim Pedid 1, 15 Is it reasonable to try to integrate promises with other promise-like things? There is no need for a special file loader that returns pledges; we can use pledges with existing callback mechanisms easily.
In setReady self , the naming could be better, if self is a pledge , why not call the parameter pledge? All in all, I like the code, it is more readable than other Promise libraries that I tried to grok. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service , privacy policy and cookie policy , and that your continued use of the website is subject to these policies. Questions Tags Users Badges Unanswered.
Promise-like entities design goals experiment. Thinking of promises as a pattern and not a contract , I figured they basically amount to the following if I'm missing the point, please correct me: They allow sensible error handling on async code. They are composable, and can be chained. I thought it would be interesting to learn: What improvements can be made on a promise-like pattern? Is it reasonable to try to integrate promises with other promise-like things? Static methods Use this instead of invoking the constructor.
Instance methods Use these on objects returned from Pledge. All other callbacks behave as follows: Experiments There are a few experimental features that aren't really the core focus of the library, but might stick. Examples The go function is useful because it can pass multiple values to the next entity in the chain, while return can only pass a single value. If you just want to do ad-hoc stuff without using special interfaces, you have to do something awkward like this: To use promises with something that passes the callback multiple arguments, you'd have to do even more work: Implementation It's sort of rough in some parts as I'm still developing the idea, but here it is.
Passed in as the first argument to a vow function. Accepts any number of arguments to pass to the next pledge. Here's my attempt to rewrite what you're trying to do. You may need to wrap the first in a Promise. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service , privacy policy and cookie policy , and that your continued use of the website is subject to these policies.
Sequelize JS node updating multiple entities with new data from previous promise Ask Question. Ando Rza 8 4.
Change false to error. Jim Pedid 1, 15 Ok, I'll take the time to redo everything not chaining callbacks.
This might take a moment. This works like a charm, struggled a bit with with last then call but it seems perfect. Thank you very much! Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Post Your Answer Discard By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service , privacy policy and cookie policy , and that your continued use of the website is subject to these policies.