0

my function delete data from store but at the same time i want to save the deletion time so i tried use return twice is right like i wrote in the function or it cause any problem later ? thank for help

function deleteStudent( $indexedDB, student, $scope, studentId, userId) {
    return $indexedDB.openStore('student', function (store) {
        student["delete_time"] = new Date().toISOString();
        return saveItemInStore(store, student, userId);

        return store.delete(studentId);
    });
}
george
  • 97
  • 1
  • 12

3 Answers3

0

The only problem this will cause is that the second return statement will never execute.

Possibly you could re-write the return statement like this...

return (saveItemInStore(store, student, userId) && store.delete(studentId));

This will return "true" if both statements succeed. Additionally "store.delete" will not execute if "saveItemInStore" fails. You'll need to determine if this is appropriate for your situation.

user1751825
  • 4,029
  • 1
  • 28
  • 58
  • in both cases mine and yours the store is not excuted, maybe aboth expression are identical and the first return only excuted – george Mar 03 '16 at 00:27
  • What does the saveItemInStore function actually return? If the store.delete is not executing in my suggested code, then it means saveItemInStore's return value is being interpreted as false. – user1751825 Mar 03 '16 at 00:30
  • Both expressions aren't identical. In yours the second statement can never execute. In mine it will execute if the first returns a "truthy" value. – user1751825 Mar 03 '16 at 00:32
  • An explanation of a "truthy" value can be found at https://developer.mozilla.org/en-US/docs/Glossary/Truthy – user1751825 Mar 03 '16 at 00:39
0

Objects are tailor-made to allow you to return multiple pieces of information from a function. Just create as many properties as desired on a single object and return that object. Of course, the code that calls the function needs to know that that is what is being returned and use it accordingly.

var desiredInfo = myFunc();

document.write("<p>I learned both to " + desiredInfo.first + " and to " + desiredInfo.second + ".</p>");


function myFunc() {

  var objToReturn = {};

  var someAdvice = "buy low";
  objToReturn.first = someAdvice;

  var moreAdvice = "sell high";
  objToReturn.second = moreAdvice;

  return objToReturn;

}
Andrew Willems
  • 11,880
  • 10
  • 53
  • 70
0

Perhaps something like this fits your bill.

function mytestfunc(){
    alert("I will be called first")
    return (function(){
    alert("I will be called second");
    //saveItemInStore placeholder
    return (function(){
        alert("I will be called third")
      //store.delete placeholder
    })();
  })();
}

mytestfunc();

Additionally, for reference check this stackoverflow question to know more about self executing functions.

Community
  • 1
  • 1
Ashwin Krishnamurthy
  • 3,750
  • 3
  • 27
  • 49