3
 yield action.user.delete() && database.ref(url).remove()

I need to use call() in both functions in redux-sagas using Firebase functions. In action.user.delete(), I can't do yield {delete} = action.user or call([action.user, delete]), because delete is a reserved word. And in database.ref(url).remove(), I don't know how to use call() for chained functions.

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
Jose Alves
  • 142
  • 1
  • 10

1 Answers1

3

There are a couple syntax options for dealing with delete being a reserved word:

  • Pass a string for the function name to the call method
  • Get a reference to the delete function in a variable with a different name

Both options are shown below.

For chained functions, you just split the chain into the separate calls and use the result from one call to execute the next call. I have included an example of doing chained calls after the two delete examples.

import { call } from "redux-saga/effects";

const refResponse = {
  remove: () => {
    console.log("You removed me!");
  }
};
const action = {
  user: {
    delete: () => {
      console.log("You deleted me!");
    }
  }
};
const database = {
  ref: () => {
    console.log("Returning ref");
    return refResponse;
  }
};
export const starter = function*() {
  console.log("before call");
  yield call([action.user, "delete"]);
  console.log("after call using string for function name");
  const deleteFunc = action.user.delete;
  yield call([action.user, deleteFunc]);
  console.log("after call using function variable");

  console.log("before start of chained call");
  const result = yield call([database, "ref"]);
  console.log("before 2nd part of chained call");
  yield call([result, "remove"]);
};

Edit 9470nnlwmy

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
  • sorry, but it does not work for me.. I got it done using `const {ref} =database const {remove} = database.ref() const result = yield call([database, ref], url) yield call([result, remove])` and `const deleteFunction = () => action.user.delete() yield call([action.user, deleteFunction])`, but I thank you for your help, big hug. – Jose Alves Jan 19 '19 at 01:43