简体   繁体   English

在不使用回调的情况下,将嵌套函数中的值返回给它的父级

[英]Returning a value from a nested function to it's parent without using a callback

I wrote the following function to check if my HTML5 openDatabase table is filled or empty: 我编写了以下function来检查我的HTML5 openDatabase表是填充还是空:

var that = this;
that.db = openDatabase('dbname', '1.0', "description", 1024 * 1024);

that.tableFilled = function( tableName ) {

    that.db.transaction(function ( tx ) {

        tx.executeSql('SELECT * FROM ' + tableName, [],
            function success( c, results ) {
                return ( results.rows.length > 0 ? true : false );
            },
            function fail() {
                console.log('FAiL');
            }
        );

    });

};

I am trying to return the true or false values to tableFilled() . 我试图将truefalsereturntableFilled()

Actually that.tableFilled('tableName') returns undefined . 实际上that.tableFilled('tableName')返回undefined

What I am trying to achieve at the end is: 我最终想要实现的目标是:

if ( that.tableFilled('tableName') ){
    // ...
}

Is there a way I can return the true or false values to the parent function tableFilled() without using a callback ? 有没有办法可以在不使用回调的情况下truefalsereturn到父函数tableFilled()

You're dealing with an asynchronous process so you can't return a value directly. 您正在处理异步进程,因此无法直接返回值。

What you can do however is return a promise . 但是什么你能做的就是回报的承诺 Your function will promise to give you that value when it's available. 您的功能将承诺在可用时为您提供该值。 To get the value out of the promise, you have to add a callback function. 要从承诺中获取值,您必须添加回调函数。

You still need to use a callback function but you don't need to nest your functions anymore, you can just serialize them. 您仍然需要使用回调函数,但不再需要嵌套函数,只需序列化它们即可。

This may be way out of scope for your current needs but it's a very interesting concept. 这可能超出了您当前需求的范围,但这是一个非常有趣的概念。 Just Google for it if you want to know more. 如果你想了解更多信息,只需谷歌吧。

Here is a short example: 这是一个简短的例子:

function my_function() {
    var promise = new_promise();
    do_asynchronous(function callback(result) {
        promise.resolve(result); // gets called after 1 second
    });
    return promise;
}

var promise = my_function();
promise.done(function(result) {
    console.log(result);    // prints "yay!" after 1 second
});

function new_promise() {
    var handlers = [];
    return {
        "resolve": function (result) {
            for (var i = 0; i < handlers.length; i += 1) {
                handlers[i](result);
            }
        },
        "done": function (a_callback) {
            handlers.push(a_callback);
        }
    };
}

function do_asynchronous(callback) {
    setTimeout(function () {
        callback("yay!");
    }, 1000);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用嵌套回调的值作为父函数的返回 - Use Value of a Nested Callback as Return for Parent Function 从回调函数返回值 - Returning a value from callback function 使用ajax和回调函数向/从函数传递/返回值 - Passing/returning value to/from a function using ajax and callback function 如何从嵌套函数设置父函数的变量值? - how to set parent function's variable value from nested function? 如何从嵌套函数回调中返回一个值作为父函数的返回值? - How to return a value from a nested function callback as the return value of parent function? React 嵌套子组件回调函数无法读取父组件的状态(使用 Hooks) - React nested child component callback function cannot read parent's state (using Hooks) 从React中父组件的回调函数中的子组件访问变量,并在父组件中呈现返回的值 - Access variables from a child component in parent component's callback function in React and render the returned value in parent 检查函数是使用回调还是返回值 - Check if a function is using a callback or returning a value 从距离矩阵回调 function 未定义返回值? - Returning a value from distancematrix callback function undefined? 从Google Maps回调函数返回值 - Returning value from a google maps callback function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM