Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Run a list of functions in order in a given object context. The functions can be callback-taking or promise-returning.

License

NotificationsYou must be signed in to change notification settings

isaacs/function-loop

Repository files navigation

Run a list of synchronous or asynchronous functions, and call afunction at the end.

USAGE

import{loop}from'function-loop'// or `const { loop } = require('function-loop')constloop=require('./dist/cjs/index.js').default// synchronous usageconstlist=[()=>console.log(1),()=>console.log(2),()=>console.log(3),]constresult=loop(list,()=>{console.log('done')returntrue},(er)=>{console.error('threw somehow',er)})console.log('result:',result)// logs:// 1// 2// 3// done// result: true// asynchronous usageconstplist=[async()=>console.log(1),async()=>newPromise(resolve=>setTimeout(resolve,100)).then(()=>console.log(2)),async()=>console.log(3),]constpresult=loop(plist,()=>{console.log('done')returntrue},(er)=>{console.error('threw somehow',er)})console.log('result:',presult)presult.then(()=>console.log('resolved'))// logs:// 1// result: Promise { <pending> }// 3// 2// resolved

Zalgo Preserving

This module is"zalgo-preserving",meaning that synchronous returns will result in a sync call tothe supplied cb, and async calls will result in the done callbackbeing called asynchronously. The loop will return a Promiseindicating when it is finished, if any async functions areencountered. It does not artificially defer if functions arecalled synchronously.

API

loop(functionList, doneCallback, errorCallback)

Run all the functions and then call thedoneCallback or calltheerrorCallback if there are any errors.

Functions are called without being bound to any object asthis.

Functions can return a Promise to do async operations, or not ifthey are done synchronously. Throws and Promise rejection arereported to theerrorCallback provided.

Return value is the return value of the callback, or aPromiseresolving to the callback's return value if any of the functionsin the list return promises.

About

Run a list of functions in order in a given object context. The functions can be callback-taking or promise-returning.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp