GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - thunks/thunk-loop: Asynchronous tasks loop (while (true) { ... }).

This repository was archived by the owner on Aug 17, 2023. It is now read-only.
BranchesTags
Open more actions menu

Latest commit

 

History

10 Commits
10 Commits

Folders and files

NameName
Last commit message
Last commit date
examples
examples
 
 
test
test
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
index.js
index.js
 
 
package.json
package.json
 
 

Repository files navigation

thunk-loop

Asynchronous tasks loop (while (true) { ... }).

NPM version Build Status Downloads

thunks

Demo

Infinite Clock:

const thunk = require('thunks')()
const thunkLoop = require('..')

var i = 0
thunkLoop(function *() {
  yield thunk.delay(1000)
  i = ++i % 60
  console.log(i)
  return true
})(function (_) {
  // will never reach here.
})

API

const thunkLoop = require('thunk-loop')

thunkLoop(iter[, errorHandle])

return thunk function. You should run the thunk function because of thunk's lazy evaluation. If iter is infinite, you will not get the last result except error occured.

  • iter: {Function}, it is your task loop, can be sync or async task. If iter's result is true, the loop will continue, otherwise the loop will terminate.

    Sync function:

    var i = 1000
    thunkLoop(function () {
      if (--i) return true
      return 'OK'
    })(function (err, res) {
      console.log(err, res, i) // null, 'OK', 0
    })

    Promise:

    var i = 1000
    thunkLoop(function () {
      if (--i) return Promise.resolve(true)
      return Promise.resolve('OK')
    })(function (err, res) {
      console.log(err, res, i) // null, 'OK', 0
    })

    Generator function:

    var i = 1000
    thunkLoop(function *() {
      // yield thunk or promise
      if (--i) return yield thunk(true)
      return yield Promise.resolve('OK')
    })(function (err, res) {
      console.log(err, res, i) // null, 'OK', 0
    })
  • errorHandle: {Function}, it is optional, can be sync or async function. It is used to catch the exception from iter.

    If errorHandle omit. the exception from iter will terminate loop:

    thunkLoop(function *() {
      throw new Error('error!')
    })(function (err, res) {
      console.log(err.message, ) 'error!'
    })

    If errorHandle return true. the exception from iter will ignore and loop will continue:

    var i = 1000
    thunkLoop(function () {
      if (--i) throw new Error('test')
      return 'OK'
    }, function (err) {
      console.log(err.message) // 'test'
      return true
    })(function (err, res) {
      console.log(err, res, i) // null, 'OK', 0
    })

    If errorHandle throw error:

    var i = 1000
    thunkLoop(function () {
      if (--i) throw new Error('test')
      return 'OK'
    }, function (err) {
      console.log(err.message) // 'test'
      throw new Error('errorHandle error')
    })(function (err, res) {
      console.log(err.message, i) // 'errorHandle error', 999
    })

About

Asynchronous tasks loop (while (true) { ... }).

Resources

Readme
MIT license
Activity
Custom properties

Stars

2 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.