Return to site

ANGULAR INTERVIEW QUESTION: How does subscribe() method works?

· ngInterview,fullstack

First, let's remind that an Observable publishes values only when we subscribe to it.

So to subscribe to such stream, you have to invoke its subscribe() method, and we parse to it an observer.

 

Usually we do not pass the observer but a command line:

 

It is a coding shortcut.

𝐒𝐨, 𝐭𝐡𝐞 𝐬𝐮𝐛𝐬𝐜𝐫𝐢𝐛𝐞 𝐦𝐞𝐭𝐡𝐨𝐝 𝐢𝐬 𝐚 𝐦𝐞𝐭𝐡𝐨𝐝 𝐨𝐟 𝐎𝐛𝐬𝐞𝐫𝐯𝐚𝐛𝐥𝐞𝐬, 𝐰𝐡𝐢𝐜𝐡 𝐭𝐚𝐤𝐞𝐬 𝐚𝐧 𝐎𝐛𝐬𝐞𝐫𝐯𝐞𝐫 𝐚𝐬 𝐩𝐚𝐫𝐚𝐦 𝐚𝐧𝐝 𝐝𝐞𝐚𝐥𝐬 𝐰𝐢𝐭𝐡 𝐝𝐢𝐟𝐟𝐞𝐫𝐞𝐧𝐭 𝐧𝐨𝐭𝐢𝐟𝐢𝐜𝐚𝐭𝐢𝐨𝐧𝐬 𝐫𝐞𝐜𝐞𝐢𝐯𝐞𝐝 𝐛𝐲 𝐭𝐡𝐞 𝐬𝐭𝐫𝐞𝐚𝐦.