Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jimmywarting/lazy-resolver

Skip hoops with promises, make async code sync-ish looking (it is still async)
https://github.com/jimmywarting/lazy-resolver

async chain javascript promise

Last synced: 25 days ago
JSON representation

Skip hoops with promises, make async code sync-ish looking (it is still async)

Awesome Lists containing this project

README

        

# lazy-resolver
Skip hoops with promises

```bash
npm install lazy-resolver
```
Do you know how lodash _.get works or angulars $parse works?
this lib is kind of like that, you can test deep objects/path
but it dose it with promises and by chaining a dummy function
to evaluate the code when a promise has been resolved

# Example:

```js
const resolve = require('lazy-resolver') // This is a Proxy handler
const fetch = resolve(import('node-fetch')).default

fetch(url)
.then(response => console.log(response))
```

Go beond what's possible and make promise chain sync-ish looking
```js
// Node variant
const fetch = resolve(import('node-fetch')).default
fetch('https://httpbin.org/get?items=4&items=2')
.json()
.args
.items
.map(n => ~~n * 4)
.then(console.log, console.warn)

// Browser variant
resolve(window)
.fetch('https://httpbin.org/get?items=4&items=2')
.json()
.args
.items
.map(n => ~~n * 4)
.forEach(n => console.log(n))
```

Test if obj exist, similar to lodash/get or optional chaining
```js
const obj = {}
exist = await resolve(obj).foo.bar().buz[28]
```