Skip to content

Instantly share code, notes, and snippets.

@thybzi thybzi/test.js
Created Jul 20, 2018

Embed
What would you like to do?
async/await & class
class Test {
constructor() {
this.processData({}).then((data) => {
console.log(data);
})
}
async processData(data) {
data = await this.one(data);
console.log('one');
data = await this.two(data);
console.log('two');
data = await this.three(data);
console.log('three');
return data;
}
one(data) {
return new Promise((resolve, reject) => {
setTimeout(() => {
data.foo = 11;
data.bar = 42;
resolve(data);
}, 2000);
});
}
two(data) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (data.hasOwnProperty('foo')) {
Object.keys(data).forEach((key) => {
data[key] = 2 * data[key];
});
resolve(data);
} else {
reject('wtf');
}
}, 500);
})
}
three(data) {
return new Promise((resolve, reject) => {
setTimeout(() => {
data.qux = Object.keys(data).reduce((result, key) => {
return result + data[key];
}, 0);
resolve(data);
}, 1000);
})
}
}
module.exports = new Test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.