Skip to content

Instantly share code, notes, and snippets.

@mpj mpj/obs.js
Created May 24, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.