$ npm install bloody-promise
var promise = require("bloody-promise")
creates a new promise and immediately executes fn
(asynchronously) with the promise as first argument.
fn
has two function arguments, resolve
and reject
which set promise state.
adds callbacks to promise
and returns a new promise based on the return values of callbacks.
If a promise is returned from the executed callback, new promise
will be fulfilled
or rejected
when this promise is fulfilled
or rejected
.
.then(null, rejectCallback)
shorthand.
NOTE : ES3 environments may require to use a ["catch"]
syntax.
.then(callback, callback)
shorthand.
resolves a promise with value
rejects a promise with reason
creates a promise resolved when all array
promises are fulfilled.
gives the returned promise the state of the first done promise in the array.
events can be used using .on(event, cb)
resolve
: when the promise is resolvedreject
: when the promise is rejectederror
: when an error occured in a.then
callbackdone
: when the promise is resolved or rejected
var promise = require("bloody-promise")
function firstClick(){
var click = promise.create(function(resolve){
document.documentElement.addEventListener("click", onClick, false)
function onClick(eventObject){
click.resolve({
eventObject.pageX,
eventObject.pageY,
})
document.documentElement.removeEventListener("click", onClick, false)
}
})
return click
}
firstClick()
.then(function(coords){
console.log("user click on", target)
tracking.push(JSON.stringify(coords))
})
.then(function(){
page.initEvents()
})