Skip to content

Instantly share code, notes, and snippets.

@sodastsai
Last active October 19, 2018 22:46
Show Gist options
  • Save sodastsai/b03459e0bee1a18ef759b418d394f68e to your computer and use it in GitHub Desktop.
Save sodastsai/b03459e0bee1a18ef759b418d394f68e to your computer and use it in GitHub Desktop.
promise.js
const Time = require('Time');
const Diagnostics = require('Diagnostics');
let promise = new Promise((resolve, reject) => {
Time.setTimeout(function() { resolve(1); } , 5000);
// Time.setTimeout(function() { reject("Failed"); }, 5000);
});
promise.then(function(value) {
Diagnostics.log("Then: " + value);
}).catch(function(reason) {
Diagnostics.log("Catch: " + reason);
});
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
}
let r = new Rectangle(10, 20);
Diagnostics.log(r.area);
function resolveAfter2Seconds() {
return new Promise(resolve => {
Time.setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
Diagnostics.log('calling');
var result = await resolveAfter2Seconds();
Diagnostics.log(result);
// expected output: 'resolved'
}
asyncCall();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment