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
| import { expect } from 'chai' | |
| describe('Stuff', () => { | |
| // -------THE CALLBACK WAY------- // | |
| it('looks ugly with callbacks', (done) => { | |
| User.count({}, (err, count) => { | |
| if (err) { return done(err) } | |
| expect(count).to.be.above(1) | |
| User.findOne({}, (err, user) => { | |
| if (err) { return done(err) } |
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
| // ----- Without async ----- // | |
| function regularPlusOne (i: number): Promise<number> { | |
| return new Promise(resolve => { | |
| resolve(i + 1) | |
| }) | |
| } | |
| // ----- With async ----- // | |
| async function plusOne (i: number): Promise<number> { | |
| return i + 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
| async function notSure (userId: string | null): Promise<null | User> { | |
| if (!userId) { return null } // e.g. return void | |
| const user = await User.findById(userId) // this return the result of the promise of findById | |
| if (user) { return user } | |
| throw 'User not found' | |
| } |
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
| async function demo (userId: string) { | |
| try { | |
| const user = await User.findById(user) | |
| const result = await doSomethingAsync(user) | |
| finalResult(user, result) | |
| } catch (e) { | |
| dealWithIt(e) | |
| } | |
| } |
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 demoPromise (userId) { | |
| User | |
| .findById(userId) | |
| .then(user => { | |
| doSomethingAsync(user) | |
| .then(result => { | |
| finalResult(user, result) | |
| }) | |
| }) | |
| } |
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 demoPromise (userId) { | |
| User | |
| .findById(userId) | |
| .then(doSomethingAsync) // doSomething will receive `user` as argument if everything goes smoothly | |
| .catch(dealWithIt) // here we catch both the `findById` and `doSomethingAsync` errors at once | |
| } |
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 demoCb (userId) { | |
| User.findById(userId, (err, user) => { | |
| if (err) { dealWithIt(err) } | |
| doSomethingAsync(user, (err, result) => { | |
| if (err) { dealWithIt(err) } | |
| // and so it goes... | |
| }) | |
| }) | |
| } |
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
| 1 | chess | 1.2 | |
|---|---|---|---|
| 2 | chess | 1.7 | |
| 1 | go | 1.4 | |
| 2 | go | 1.0 | |
| 1 | hide-and-seek | 8.0 | |
| 3 | hide-and-seek | 2.7 |
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
| #!/usr/bin/env ruby | |
| class Reducer | |
| attr_accessor :key, :game_type, :user_id | |
| def initialize(key,value) | |
| @key = key | |
| #split the primary key to get user_id and game type: | |
| @game_type,@user_id = key.split("|") |
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
| chess|1 1 | |
| chess|1 1 | |
| chess|2 1 | |
| chess|2 2 | |
| go|1 2 | |
| chess|1 1 | |
| go|1 1 | |
| go|1 1 | |
| chess|2 2 | |
| go|1 1 |