@simplessor/p

一种对JS Promise的简单扩展

Usage no npm install needed!

<script type="module">
  import simplessorP from 'https://cdn.skypack.dev/@simplessor/p';
</script>

README

@simplessor/p

一种对JS Promise的简单扩展

iterator(dataList, promiseGenerator)

迭代器 按dataList顺序调用promiseGenerator 并按照顺序执行生成的总promise, 最终结果通过 resultserrors 于总promise.then返回

const P = require("@simplessor/p")
const data = [100, 60, 50, 55, 75, 80, 33, 46, 99]
const generatePromise = data => new Promise((resolve, reject) => {
    setTimeout(() => {
        console.log(data)
        if (data >= 60)
            resolve(data)
        else
            reject(data)
    }, 1000)
})
P.iterator(data, generatePromise)
    .then(({
        results,
        errors
    } = result) => {
        console.log("results", results)
        console.log("errors", errors)
    }).catch(error => {
        console.log(error)
    })
    // 运行结果
    // 11:11:06 100
    // 11:11:07 60
    // 11:11:08 50
    // 11:11:09 50
    // 11:11:10 55
    // 11:11:11 75
    // 11:11:12 80
    // 11:11:13 33
    // 11:11:14 33
    // 11:11:15 46
    // 11:11:16 99
    // results [ 100, 60, 75, 80, 99 ]
    // errors [ 50, 50, 55, 33, 33, 46 ]