Radical Async Resolution. ✌️
No nonsense object resolver.
Viva la Resolution!
npm i viva.la --save
Viva.la generates a function that will crawl an object and resolve any function values or promises into an object with identical keys.
import viva from 'viva.la';
function resolveIn(seconds) {
return str => new Promise(
resolve => setTimeout(resolve, seconds * 1000, str),
);
}
const myResolution = {
viva() {
return resolveIn(5)('fight');
},
la() {
return resolveIn(1)('the');
},
resolution() {
return resolveIn(2)('power');
}
};
viva(myResolution).then(console.log);
// 5 seconds later:
// prints {viva: 'fight', la: 'the', resolution: 'power'}
Using the resolver
top level composer, you can set a context that all methods have access to.
import { resolver } from 'viva.la';
const vivaLa = resolver({count: 1});
function resolveIn(seconds) {
return str => new Promise(
resolve => setTimeout(resolve, seconds * 1000, str),
);
}
const myResolution = {
viva(context) {
console.log(context.count ++);
return resolveIn(5)('fight');
},
la(context) {
console.log(context.count ++);
return resolveIn(1)('the');
},
resolution(context) {
console.log(context.count ++);
return resolveIn(2)('power');
}
};
vivaLa(myResolution)
.then(console.log);
// 1
// 2
// 3
// then 5 seconds later:
// prints {viva: 'fight', la: 'the', resolution: 'power'}
You can pass anything as context here.
const vivaLa = resolver(() => console.log('Groovy'));
const myResolution = {
viva(sayGroovy) {
sayGroovy();
return 'fight';
},
la(sayGroovy) {
sayGroovy();
return 'the';
},
resolution(sayGroovy) {
sayGroovy();
return 'power';
}
};
vivaLa(myResolution);
// Groovy
// Groovy
// Groovy
The default resolver implementation uses an empty object for context.
Methods are called with the arguments context, {path, depth}
.
import viva from 'viva.la';
const myResolution = {
viva: {
la: {
resolution(context, {path, depth}) {
console.log('Depth', depth);
console.log('Path', path);
return true;
}
}
}
};
viva(myResolution)
.then(console.log);
// Depth 3
// Path [ 'viva', 'la', 'resolution' ]
// { viva: { la: { resolution: true } } }
TODO
TODO
TODO
TODO
MIT