Skip to content

Instantly share code, notes, and snippets.

@mpj
Created May 24, 2019 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mpj/96bd00c01c0a775d695e450287920cb1 to your computer and use it in GitHub Desktop.
Save mpj/96bd00c01c0a775d695e450287920cb1 to your computer and use it in GitHub Desktop.
const delay = require('delay')
async function* makeCountingObservable() {
let count = 0
while(true) {
if (count > 4) return
await delay(1000)
yield count++
}
}
const counter = makeCountingObservable()
;(async function() {
let lastIterat
while(true) {
lastIterated = await counter.next()
if (lastIterated.done) return
console.log(lastIterated.value)
}
for await (const num of counter) {
console.log(num)
}
})()
const greetings = ['hej', 'oi', 'hello']
for(const greeting of greetings) {
console.log(greeting)
}
function makeCountingStream() {
let count = 0
let handler = null
setInterval(function() {
count++
handler(count)
}, 500)
return {
each: function(callback) {
handler = callback
}
}
}
const stream = makeCountingStream()
stream.each(function(message) {
console.log(message)
})
function makeValueAfterOneSecondPromise() {
return {
then: function(callback) {
setTimeout(function() {
callback("hello")
}, 1000)
}
}
}
const promise = makeValueAfterOneSecondPromise()
promise.then(function(message) {
message //?
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment