Q: Promise resolution I am new to javascript promises, I am using below code from NativeScript to query sqlite database and just want to return true if row exists and false otherwise:
function hasBookmark(url) {
    (new Sqlite("pakjobs.db")).then(db => {
        db.all("SELECT url FROM bookmarks WHERE url=?", [url]).then(rows => {
            return rows.length ? true : false;
        }, error => {
            console.log("hasBookmark ERROR", error);
            return false;
        });
    }, error => {
        console.log("hasBookmark ERROR", error);
        return false;
    });

    return false;
}

However function ALWAYS returns false.
Can anybody tell how do I return true if row exists and false otherwise ?
Thanks for the help

A: You are not going to be able to return true or false, that's pretty much the point of Promises, you can't return something asynchronously in javascript.
What you can do, however, is directly returning the Promise you're creating and then calling .then on it.
function hasBookmark(url) {
    return (new Sqlite("pakjobs.db")).then(db => {
        return db.all("SELECT url FROM bookmarks WHERE url=?", [url]).then(rows => {
            return rows.length ? true : false;
        }, error => {
            console.log("hasBookmark ERROR", error);
            return false;
        });
    }, error => {
        console.log("hasBookmark ERROR", error);
        return false;
    });

}

const hasBookmarkPromise = hasBookmark('someurl');

hasBookmarPromise.then(value => {
    if (value === true) {
        console.log('has bookmarks');
    }
});


A: function hasBookmark(url) {
    return new Promise((resolve, reject) => {
        (new Sqlite("pakjobs.db")).then(db => {
            db.all("SELECT url FROM          bookmarks WHERE url=?", [url]).then(rows => {
                resolve(rows.length ? true : false)
            }, error => {
                console.log("hasBookmark ERROR", error);
                resolve(false);
            });
        }, error => {
            console.log("hasBookmark ERROR", error);
            resolve(false);
        });
    });
}

hasBookmark(url).then(function(isValid){
   If(isValid){
     // Do something
   }
});

