How to return value from debounced function in javascript?

Victor Marchuk

I have a code like that:

var originalFunction = function() {
    return 'some value';
};

var debouncedFunction = _.debounce(originalFunction, 3000);

console.log('debouncedFunction() result: ', debouncedFunction());

console.log('originalFunction() result: ', originalFunction());

(codepen link)

And the result in the console is:

debouncedFunction() result:  undefined 

originalFunction() result:  some value

As you can see, the debounced function doesn't return anything. I understand that it's caused by an internal timer in the debounced function, but is there away around that?

pwolaq

that's because debounced functions are called asynchronously - you can't return a value from them, although you can call another function passing the result:

var originalFunction = function() {
    console.log('some value');
    // or something like: callback(result)
};

var debouncedFunction = _.debounce(originalFunction, 3000);

console.log('debouncedFunction() result: ', debouncedFunction());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript - return value from function

how to return function value in javascript

How do I return a value from a function in JavaScript to a textbox in HTML

How to access a return value from a javascript async function

How to return a value from a async function in array.map in javascript?

How to return value from Javascript function to C# Blazor? [Resolved]

How to return value from this function?

How to return a value from a function

How to return a value from a function if no value is found

Return value from nested function in Javascript

jQuery/Javascript print the value from return function

Return value from the prototype function in javascript

Javascript return value from asynchronous function

Return a value from nested function in Javascript

JavaScript Promise return value from function

How I can call javascript function and get the return value from javascript function

How to return value in a function with a single line in Javascript?

How to use a return value in another function in Javascript?

how to return value of nested function in javascript?

How to return a value to an outer Javascript function

How to return a value from a function which is passed to another function as a parameter in javascript?

How to correctly return value from function?

How to return a value from a function in Java?

How do I return value from this function

How to return by value from native function?

How to return a value from a function of type `LPCTSTR`?

How to return value from function to main thread?

How to return custom value from function call?

How to get a return value from a connect function