Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: fibo is not defined #52

Closed
poliveira89 opened this issue Mar 26, 2015 · 2 comments
Closed

ReferenceError: fibo is not defined #52

poliveira89 opened this issue Mar 26, 2015 · 2 comments

Comments

@poliveira89
Copy link

I have a small file threads.js based on this example the content of the file is:

var threads = require('webworker-threads'),
    sleep = require('sleep').sleep;

var random = function(low, high) {
    return Math.floor(Math.random() * (high - low + 1) + low);
};
function fibo(n, low, high) {
    var rnd = random(low, high);
    sleep(rnd);
    return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
}

var threadOne = threads.create(),
    threadTwo = threads.create(),
    threadThree = threads.create();
threadOne.eval('fibo(10, 60, 120)', function(err, result) {
    if (err) { console.error(err); }

    console.log('[ thread-1 ] result: %s', result);
});
threadTwo.eval('fibo(10, 60, 120)', function(err, result) {
    if (err) { console.error(err); }

    console.log('[ thread-2 ] result: %s', result);
});
threadThree.eval('fibo(10, 60, 120)', function(err, result) {
    if (err) { console.error(err); }

    console.log('[ thread-3 ] result: %s', result);
});

When I execute node threads.js:

vagrant@vagrant-ubuntu-trusty-64:~/nodejs$ node threads.js
[Error: ReferenceError: fibo is not defined]
[ thread-2 ] result: null
[Error: ReferenceError: fibo is not defined]
[ thread-3 ] result: null
[Error: ReferenceError: fibo is not defined]
[ thread-1 ] result: null

For every thread I receive the error [Error: ReferenceError: fibo is not defined] but as we can see I have the function declared as the example and for an experiment, I changed to var fibo = function(n, low, high) { ... } still did not worked.

@lucnap
Copy link

lucnap commented Apr 7, 2015

require('sleep') is the problem
You can't require external libraries.
The solution is to put all your functions and external libraries in a file, browserify that file and then make a thread.load() of that browserified file.

@poliveira89
Copy link
Author

I have found that use require on the code block that will be loaded on thread.load does not work.

But your suggestion with browserify, could be a great tip! Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants