new Promise(task)
Promise pattern for calling asynchronous code, usually ajax/setTimeout/setInterval
Parameters:
| Name | Type | Description |
|---|---|---|
task |
function | Task to do asynchronously |
Example
var promise = html.Promise(function(resolve, reject) {
// We visualize a task that run on server for long time.
// In real world application, the task is usually an ajax call
setTimeout(function() {
var success = prompt("Should be done or fail this task?", 1, 0);
if (success == '1') {
resolve("Ok, we got server's response");
} else {
reject("Fail to get response from server. Bad request (lol).");
}
}, 2000);
});
promise
.done(function(data) {
console.log(data);
}).done(function(data) {
console.log(data, 'done the second call');
}).fail(function(reason) {
console.log(reason);
}).fail(function(reason) {
console.log(reason, 'fail the second call');
});
Members
-
doneActions
-
All done action callbacks
-
failActions
-
All fail action callbacks
Methods
-
done(action)
-
Set done callback
Parameters:
Name Type Description actionfunction Done action callback
Returns:
promise - Return the promise itself
- Type
- html.Promise
-
resolve(val)
-
Resolve the task with value
Parameters:
Name Type Description valObject Value to resolve