Reputation: 95
The problem with my code here is that the dom element that I am trying to get does not always exist when the code first runs, and if it does not exist then the promise will never be made.
Is it possible for me to wait for the promise to be made before I try to get it?
I want my last line of code to wait and only run when I make the promise, is this possible?
function identify_home(){
const ytd_home_page = $("ytd-browse.style-scope.ytd-page-manager")
if (ytd_home_page.length){
for (var i=0; i<ytd_home_page[0].children.length; i++){
if (ytd_home_page[i].attributes[0].value === "home"){
return new Promise(res=>{
res(ytd_home_page[i])
})
}
}
}
}
identify_home().then(res=>{
console.log(res)
})
Upvotes: 0
Views: 75
Reputation: 42297
You need to return the Promise as a result from the function. Right now you are conditionally returning undefined
or Promise
.
Edit: I'm not sure yet what OP is actually trying to do. Using a Promise in a DOM query doesn't make any sense.
Upvotes: 1