Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
Add Comment
Please, Sign In to add comment