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

wait-till

v1.0.3

Published

wait-till is library that awaits until variable matches function then will run code.

Downloads

12

Readme

Basic Usage

1st Usage

var Await = require("wait-till");

var variable = {
  "name": "Some random name"
}

setTimeout(() => {
  variable.name = "Some random name 2";
}, 3000);

(async () => {
  await Await(variable, (Variable) => Variable.name == "Some random name 2");

  //.... continue code after variable is matched
})();
//We needed to create async function to run await in it

//Your code here will wait 3 seconds until timeout will set variable.name to "Some random name 2" so your code will wait until the variable matches the function

2nd Usage

var Await = require("wait-till");

var variable = {
  "name": "Some random name"
}

var randomNameAwait = Await(variable, (Variable) => Variable.name == "Some random name 2", () => {
  variable.name = "Some random name";
})

//Now we added callback that meens it is not anymore Promise but it will use callback to call function
//Now everytime it will detect that "variable.name" is "Some random name 2" it will change it to "Some random name" and will continue awaiting

//If you would want to end this you can use
randomNameAwait.end();

3rd Usage

var Await = require("wait-till");

var variable = {

}

Await(variable, (Variable, OldVariable) => Variable !== OldVariable, (now, old) => {
  //Do things
})
//Now this will await until variable object will cahnge in any way  [<variable> (Variable state now), <oldVariable> (Variable state on last check)]
//It returns 2 arguments Variable in state now, and Variable in old State (This args are returned in all Await usages we just don't use it)
//So you can use this Old state of variable in 1st usage too

4th usage

var Await = require("wait-till");

var variable = {
  "name": "Some random name"
}

Await(() => variable.name), (Variable) => Variable == "Some random name 2", () => {
  variable.name = "Some random name";
})
//When 1st argument is declared as function it will evacuate that function first and then use it's returned value as Variable
//Function is evacuated everytime it makes check

Examples

Discord.js music bot when checking if there is any non bot member in channel so it can pause music if there is not

var Await = require("wait-till");
//We are using Promise so code can continue but when bot will detect that there is no user there it will activate itself then

new Promise(() => {
  await Await(serverQueue, (e) => e.voiceChannel.members.filter(m => !m.user.bot).size < 1);
  serverQueue.dispatcher.pause();
});
//This example runned only 1 time bot will pause dispatcher only one time when users left the channel
//Fix for this is bellow



//This is example of discord bot so we need to put this event into loop we can do that by adding callback into function
//There we don't need to use Promise becouse we are not invoking this as Promise (with "await" before calling function)

Await(serverQueue, (e) => e.voiceChannel.members.filter(m => !m.user.bot).size < 1&&!e.dispatcher.paused, () => {
  //This is callback so now we are finished
  serverQueue.dispatcher.pause();
})
//This Example will pause dispatcher everytime it detects no users in cahnnel and that dispatcher is not paused

2nd example is the same as 1st but we will resume music when noone was in channel and someone joined

Await(serverQueue, (currentQueueState, oldQueueState) => currentQueueState.voiceChannel.members.filter(m => !m.user.bot).size > 0&&oldQueueState.members.filter(m => !m.user.bot).size < 1&&e.dispatcher.paused, () => {
  //This is callback so now we are finished
  serverQueue.dispatcher.resume();
})
//This will wait until someone joins channel (CurrentQueueState voiceChannel users count is > than 0 and OldQueueSate voiceChannel users count is < than 1)
//And dispatcher is paused

3rd example is with our old good "variable.name" we will now detect if variable object was changed and if was continue code or run some function

var Await = require("wait-till");

var variable = {
  "name": "Some random name"
}

(async () => {
  var oldVariable = variable;  //An copy of variable object
  await Await(variable, (Variable) => {
    if(Variable !== oldVariable) return true; 
    else {
      oldVariable == Variable;
      return false;
    }
  })
  //If object changed continue execution of code
})()

//We are again using async function so await can be called
var Await = require("wait-till");

var variable = {
  "name": "Some random name"
}

Await(variable, (Variable, OldVariable) => Variable !== OldVariable, () => {
  //Code here will be executed everytime when "variable" object changes
})
//If object "variable" changed continue execution of code

//We don't need to use async function again becouse we are not using await when calling Await function
//More explained in 3rd Usage

Test it now

1st usage test This test will after 3 seconds detect that variable has changed to "Some random name 2" and will print "Text matched" into console

var Await = require("wait-till");

var variable = {
  "name": "Some random name"
}

setTimeout(() => {
  variable.name = "Some random name 2";
}, 3000);

(async () => {
  await Await(variable, (Variable) => Variable.name == "Some random name 2");
  console.log("Text matched")
})();

2nd usage test This test will detect variable change every 3 seconds and will change it back everytime and print "Text matched" to console all this works in 3 seconds interval

var Await = require("wait-till");

var variable = {
  "name": "Some random name"
}

setInterval(() => {
  variable.name = "Some random name 2";
}, 3000)

Await(variable, (Variable) => Variable.name == "Some random name 2", () => {
  variable.name = "Some random name";
  console.log("Text matched");
})