callACat(function () {
console.log('Hello, Cat!');
});
callACat(function () {
callADog(function () {
callAHorse(function () {
callSanta(function () {
console.log('Where am I?');
});
});
});
});
phone
.on('cat', function(){ phone.callADog(); })
.on('dog', function(){ phone.callAHorse(); })
.on('horse', function(){ phone.callSanta(); })
.on('santa', function(){ console.log('Santa?'); });
phone.callACat();
phone.callACat()
.then(phone.callADog)
.then(phone.callAHorse)
.then(phone.callSanta)
.then(function(){ console.log('Santa?'); });
callACat(function () {
callADog(function () {
callAHorse(function () {
callSanta(function () {
console.log('Where am I?');
});
});
});
});
phone.callACat()
.then(phone.callADog)
.then(phone.callAHorse)
.then(phone.callSanta)
.then(function(){ console.log('Santa?'); });
new Promise(function (fulfill, reject) {
phone.callACat(function (error, result) {
if (error) {
reject(error);
return;
}
fulfill(result);
});
});
Promise.resolve(jQueryPromise);
Promise.resolve(number);
Promise.resolve(string);
Promise.reject(new Error('Cat not found'));
phone.callACat()
.then(function (catResult) {
console.log(catResult);
return phone.callADog(); // Promise
})
.then(function (dogResult) {
console.log(dogResult);
return 'Hello, Dog!'; // Value
})
.then(function (msg) { console.log(msg); }); // Hello, Dog!
phone.callACat()
.then(function () {
throw new Error('No cat!');
})
.then(function () {
console.log('This is a cat!'); // skipped
})
.catch(function (reason) {
console.error(reason); // No cat!
});
phone.callACat()
.then(function () {
throw new Error('No cat!');
})
.then(function () {
console.log('This is a cat!'); // skipped
}, function (reason) {
console.error(reason); // No cat!
}); // for old browsers no .catch
phone.callACat('Tom')
.then(function () {
throw new Error('No cat!')
})
.catch(function (reason) {
reason.message += 'Tom';
throw reason;
});
Promise.all([
phone.callACat(),
phone.callADog(),
phone.callAHorse()
])
.then(function (resultsList) {
console.log('Everybody are online!');
});
Promise.race([
phone.callACat(),
phone.callADog(),
phone.callAHorse()
])
.then(function (value) {
console.log('Who is speaking?');
});