npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

run-in-thread

v0.1.0

Published

Run a function in work thread for NodeJS

Downloads

4

Readme

Run a function in work thread

传入一个函数及对应的数据, 该函数将在worker thread执行, 并将结果以Promise方式返回给主线程. 在worker thread中执行function的简单封装, 目的时屏蔽掉worker thread库的重复写法,简化代码编写难度.

通过这种方式可以将计算密集型函数或长耗时同步处理过程,比较简单的移到工作线程中来保证主线程的持续服务.

usage

function f1(data){
 //do somthing with data ...
 return data * 10;
 //returns the ans or a Promise
 //同步执行的话将把运行结果作为主线程获得的结果, 异步的话请返回Promise
}

require('run-in-thread').runInThread(f1, 10).then(x=>{
  console.log(x); //will got 100
});

对函数的要求

传入的函数不要使用this, 也不要bind this, 因为子线程中的this指向为该线程对应上下文的global对象.

定义

/**
 * 将data作为参数调用fn,并异步返回结果
 * @param {Function} fn 待执行的函数, 直接传入函数引用即可, fn的格式为
 *   function fn(data={}){return data};
 * @param {Objectj} data 待执行函数的参数信息,任意格式
 * @returns Promise
 */
function runInThread(fn, data){
  return Promise.resolve();
}