Promise creation and usage
This feature allows users to create new promises and handle their resolution or rejection. The code sample demonstrates how to create a new promise and use 'then' and 'catch' methods to handle its outcome.
const pinkiePromise = require('pinkie-promise');
const promise = new pinkiePromise(function(resolve, reject) {
// Asynchronous operation here
if (/* operation successful */) {
resolve('Success!');
} else {
reject('Failure!');
}
});
promise.then(function(value) {
console.log(value); // 'Success!'
}).catch(function(reason) {
console.log(reason); // 'Failure!'
});
Chaining promises
This feature demonstrates how promises can be chained to perform a sequence of asynchronous operations where each step waits for the previous one to complete.
const pinkiePromise = require('pinkie-promise');
pinkiePromise.resolve(1)
.then(function(value) {
return value + 1;
})
.then(function(value) {
return value + 1;
})
.then(function(value) {
console.log(value); // 3
});