Created
February 2, 2018 09:44
-
-
Save ramsunvtech/284b368750518fac22dfbe702a006c1f to your computer and use it in GitHub Desktop.
Simple Promise
This file contains hidden or 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
| const async = require('async'); | |
| const db = require('./db'); | |
| function myAsyncParallel(callback) { | |
| async.parallel({ | |
| first: db.userData.bind(null, 'first arg'), | |
| second: db.activityData.bind(null, 'second arg') | |
| }, callback); | |
| } | |
| module.exports = { | |
| myAsyncParallel, | |
| }; |
This file contains hidden or 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
| const mocha = require('mocha'); | |
| const expect = require('chai').expect; | |
| const sinon = require('sinon'); | |
| const db = require('./db'); | |
| const asyncFile = require('./parallel'); | |
| describe('Async Parallel.js', () => { | |
| let params, resultValues, userDataStub, activityDataStub, activityDataSpy; | |
| before(() => { | |
| params = ['first arg', 'second arg']; | |
| resultValues = ['First Result', 'Second Result']; | |
| }); | |
| beforeEach(() => { | |
| userDataStub = sinon.stub(db, 'userData').callsFake((arg, callback) => { | |
| callback(null, { | |
| arg, | |
| result: resultValues[0] | |
| }); | |
| }); | |
| activityDataStub = sinon.stub(db, 'activityData').callsFake((arg, callback) => { | |
| callback(null, { | |
| arg, | |
| result: resultValues[1] | |
| }); | |
| }); | |
| }); | |
| it('should return the response', () => { | |
| return asyncFile.myAsyncParallel(function(error, results) { | |
| expect(results).to.deep.equal({ | |
| first: { | |
| arg: params[0], | |
| result: resultValues[0] | |
| }, | |
| second: { | |
| arg: params[1], | |
| result: resultValues[1] | |
| } | |
| }); | |
| }); | |
| }); | |
| }); |
This file contains hidden or 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
| function basic(result) { | |
| if (!result) { | |
| return Promise.reject('Failure'); | |
| } | |
| return new Promise((resolve, reject) => { | |
| // Assume that this below data is retrieved | |
| // from Database which takes 1.6 seconds. | |
| setTimeout(() => resolve(result), 1600); | |
| }); | |
| } | |
| module.exports = { | |
| basic, | |
| }; |
This file contains hidden or 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
| const mocha = require('mocha'); | |
| const expect = require('chai').expect; | |
| const basicFile = require('./basic'); | |
| describe('Basic Testing', () => { | |
| const noop = () => {}; | |
| describe('Basic', () => { | |
| it('should have method `basic`', () => { | |
| expect(basicFile).to.be.an('object'); | |
| expect(typeof basicFile.basic).to.equal('function'); | |
| }); | |
| // You can add more basic Test Cases. | |
| }); | |
| describe('Error Case', () => { | |
| it('should return Error message for empty param', () => { | |
| return basicFile.basic().then(noop, (errorMessage) => { | |
| expect('Failure').to.equal(errorMessage); | |
| }); | |
| }); | |
| // You can add more Error Cases. | |
| }); | |
| describe('Success Case', () => { | |
| it('should return param with Hello for valid param', () => { | |
| return basicFile.basic('Success').then((successMessage) => { | |
| expect('Success').to.equal(successMessage); | |
| }, noop); | |
| }); | |
| // You can add more Success Cases. | |
| }); | |
| }); |
This file contains hidden or 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
| function getData(payload) { | |
| return new Promise((resolve, reject) => { | |
| if (!payload) { | |
| reject({ | |
| error: true, | |
| }); | |
| } | |
| setTimeout(() => { | |
| resolve({ | |
| name1: 'value1', | |
| name2: 'value2', | |
| }); | |
| }, 5000); | |
| }); | |
| } | |
| module.exports = { | |
| getData, | |
| }; |
This file contains hidden or 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
| const mocha = require('mocha'); | |
| const expect = require('chai').expect; | |
| const sinon = require('sinon'); | |
| const db = require('./db'); | |
| const service = require('./service'); | |
| describe('service.js', () => { | |
| let noop, label, payload, response, dbStub; | |
| before(() => { | |
| noop = () => {}; | |
| label = 'emp'; | |
| payload; | |
| response; | |
| dbStub; | |
| }); | |
| beforeEach(() => { | |
| payload = { | |
| prop1: `value1-${label}`, | |
| prop2: `value2-${label}`, | |
| }; | |
| response = { | |
| name1: 'value1', | |
| name2: 'value2', | |
| }; | |
| dbStub = sinon.stub(db, 'getData').callsFake(() => { | |
| return Promise.resolve(response); | |
| }); | |
| }); | |
| afterEach(() => { | |
| dbStub.restore(); | |
| }); | |
| it('should return error response', () => { | |
| return service.getResultData().then(noop, (data) => { | |
| expect(data).to.deep.equal({ | |
| error: true, | |
| }); | |
| }); | |
| }); | |
| it('should return success response', () => { | |
| return service.getResultData(label).then((data) => { | |
| expect(data).to.deep.equal(response); | |
| }); | |
| }); | |
| }); |
This file contains hidden or 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
| (function simple() { | |
| let serviceResponse = new Promise((resolve, reject) => { | |
| setTimeout(() => resolve(‘Success’), 2000); | |
| }); | |
| serviceResponse.then((msg) => { | |
| console.log(`Hello Message is ${msg}`); | |
| }, () => { | |
| console.log(‘oops!its rejected’); | |
| }); | |
| }()); |
This file contains hidden or 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
| const db = require('./db'); | |
| function getResultData(name) { | |
| let payload; | |
| if (name) { | |
| payload = { | |
| prop1: `value1-${name}`, | |
| prop2: `value2-${name}`, | |
| }; | |
| } | |
| return new Promise((resolve, reject) => { | |
| db.getData(payload).then((data) => { | |
| if (data.error) { | |
| reject(data); | |
| } | |
| resolve(data); | |
| }); | |
| }); | |
| } | |
| module.exports = { | |
| getResultData, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment