Created
July 10, 2018 16:08
-
-
Save gregoryStarr/f2533f8e01f079befddd604ad5ed3841 to your computer and use it in GitHub Desktop.
Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 75: Promise - basics | |
// To do: make all tests pass, leave the assert lines unchanged! | |
"use strict" | |
describe('a Promise represents an operation that hasn`t completed yet, but is expected in the future', function() { | |
it('`Promise` is a global function', function() { | |
const expectedType = 'function'; | |
console.log(Promise) | |
assert.equal(typeof Promise, expectedType); | |
}); | |
describe('the constructor', function() { | |
it('instantiating it without params throws', function() { | |
const fn = () => { Promise((ok, error)=>{alert(success)}) } | |
assert.throws(fn); | |
}); | |
it('expects a function as parameter', function() { | |
const param = (ok, error)=>{}; | |
assert.doesNotThrow(() => { new Promise(param); }); | |
}); | |
}); | |
describe('simplest promises', function() { | |
it('resolve a promise by calling the `resolve` function given as first parameter', function(done) { | |
let promise = new Promise((resolve) => { | |
if(1==1){ | |
resolve(); | |
} | |
}); | |
promise | |
.then(() => done()) | |
.catch(() => done(new Error('The promise is expected to resolve.'))); | |
}); | |
it('the `resolve` function can return a value, that is consumed by the `promise.then()` callback', function(done) { | |
let promise = new Promise((resolve) => { | |
resolve(42) | |
}); | |
promise | |
.then(value => {assert.equal(value, 42); done(); }) | |
.catch(() => done(new Error('The promise is expected to resolve with 42!'))); | |
}); | |
it('rejecting a promise is done by calling the callback given as 2nd parameter', function(done) { | |
let promise = new Promise((resolve, reject) => { | |
if(!a){reject()} | |
}); | |
promise | |
.then(() => done(new Error('The promise is expected to be rejected.'))) | |
.catch(() => done()); | |
}); | |
}); | |
describe('an asynchronous promise', function() { | |
it('can resolve later, also by calling the first callback', function(done) { | |
let promise = new Promise((resolve) => { | |
setTimeout(() => resolve(), 100); | |
}); | |
promise | |
.then(() => done()) | |
.catch(() => done(new Error('The promise is expected to resolve.'))); | |
}); | |
it('reject it at some later point in time, calling the 2nd callback', function(done) { | |
let promise = new Promise((resolve,reject) => { | |
setTimeout(() => reject(), 100); | |
}); | |
promise | |
.then(() => done(new Error('The promise is expected to be rejected.'))) | |
.catch(() => done()); | |
}); | |
}); | |
describe('test library (mocha here) support for promises', function() { | |
it('just returning the promise makes the test library check that the promise resolves', function() { | |
let promise = new Promise((reject, resolve) => { | |
resolve(); | |
}); | |
// return the promise to mocha, it has the checking for promise resolving built in, when it receives a promise | |
return promise.resolve; | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment