Skip to content
forked from kolodny/webwork

Execute Web Workers without external files

License

Notifications You must be signed in to change notification settings

peterkroon/webwork

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

webwork.js

Execute Web Workers without external files

Usage is as really simple:

callableWebworker = webwork(callback);

callback takes a single data argument and should return the result
callableWebworker is a function that takes the data to send and
a node style callback ie function(err, result) {}

Example

var goodWorker = webwork(function (data) {
    return "!!" + data  + "!!";
});
var badWorker = webwork(function (data) {
    return "!!" + data + imNotDefined + "!!";
});

goodWorker("Test123", function(err, result) {
    if (err) return alert("goodWorker Errored with " + err.message);
    alert("first goodWorker returned with " + result);
});
goodWorker("Test123", function(err, result) {
    if (err) return alert("goodWorker Errored with " + err.message);
    alert("second goodWorker returned with " + result);
});
badWorker("Test456", function(err, result) {
    if (err) return alert("badWorker Errored with " + err.message);
    alert("badWorker returned with " + result);
});
alert("Since webworkers are async this should happen first.\nThe callbacks may fire in any order");

The astute may realize that they can access the event variable inside the callback.
Also since the callback is stringified and injected into the webworker, you don't have access to any vars in scope

About

Execute Web Workers without external files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published